<template>
    <div class="content">
         <div class="head">
            <span> 科室风险项</span>
            <Button style="position:relative;left:10px" type="primary" @click="saveDeptItem">保存</Button>
         </div>
         <div class="search">
      <div class="item">
        <label>风险类别</label>
        <Select v-model="itemCategoryCode" style="width:200px;" @on-change="getItemCategoryList">
            <Option v-for="item in itemCategoryList" :value="item.itemCategoryCode" :key="item.itemCategoryName" :label="item.itemCategoryName"></Option>
        </Select>
      </div>
      <Button  type="primary" @click="init" class="checkBtn">查询</Button>
    </div>
      <div>
        <Table :loading="loading" @on-selection-change="itemSelect" :height="autoheight" stripe :columns="columns" :data="data" size="small"></Table>
      </div>
    </div>
  </template>
  <script>
  export default {
    data() {
      return {
        selectedItem: [],
        defaultSelect:[],
        loading:true,
        itemCategoryCode:'',
        itemCategoryList:[],
        autoheight:400,
        columns: [
            {
                type: 'selection',
                width: 50,
                align: 'center'
            },
            {
                title: '序号',
                type: 'index',
                width: 80,
                align: 'center'
            },
            {
                title: '类别名称',
                key: 'itemCategoryName',
                align: 'center',
            },
            {
                title: '类型名称',
                key: 'itemTypeName',
                align: 'center',
            },
            {
                title: '风险项名称',
                key: 'itemName',
                align: 'center',
            }
        ],
        data: [],
        pager: { total: 0, pageNum: 1, pageSize: 20 },
      };
    },
    created() {},
    mounted() {
      this.init();
      this.getItemCategoryList();
    },
    methods: {
        getItemCategoryList(){
            this.$http.post("/api/checkItem/getItemCategoryList",{
                   
                })
                .then(res => {
                    if (res.retHead.code == "0000000") {
                        this.itemCategoryList = res.retBody;
                        this.itemCategoryList.unshift({
                            itemCategoryCode:'0',
                            itemCategoryName:'全部'
                     });
                     
                    }
            })
        },
        init() {
            this.$http
            .post("/api/checkItem/getCheckItemListAndValid", {          
                itemCategoryCode:this.itemCategoryCode,
            })
            .then(res => {
                let itemList = res.retBody;
                //this.data默认为所有的风险项，读取当前deptId拥有的风险项
                let deptId=this.$route.query.deptId;
                     this.$http.post("/api/auth/sysdeptitem/doGetList",{
                        deptId:deptId,
                    }).then(res => {
                        this.defaultSelect=res.retBody;
                        if(this.defaultSelect!=null){
                            for(var it of this.defaultSelect){
                                for(var dItem of itemList){
                                    if(dItem.itemCode==it.itemCode){
                                        dItem._checked=true;
                                    }
                                }
                             }
                        }
                        this.data = itemList;  
                        this.loading = false;
                    });
            });
      },
      itemSelect(selection){
          this.selectedItem=selection;
      },
      saveDeptItem(){
           let deptId=this.$route.query.deptId;

            if(this.selectedItem==null || this.selectedItem==""){
                this.$Message.warning({
                top: 100,
                duration: 3,
                content: "请选择风险项"
                });
                return;
            }
            var itemCodes = "";
            for(var item of this.selectedItem){
                if(""==itemCodes){
                itemCodes = item.itemCode;
                }else{
                itemCodes = itemCodes+","+item.itemCode;
                }
            }
           this.$http.post("/api/auth/sysdeptitem/saveDeptItem",{
                deptId:deptId,
                itemCodes:itemCodes,
           }).then(res => {
            // if (res.retHead.code == "0000000") {
                this.$Message.success({
                top: 100,
                duration: 3,
                content: res.retHead.message
                });
            // }
            this.$router.push({ path: '/dept'});
            });

      },
    }
  };
  </script>
  <style>
  </style>
  